package dtime

import (
	"fmt"
	"time"
)

/*
*
返回当前时间的年月日字符串信息
*/
func GetCurrentTimeYMD() string {
	now := time.Now()
	return fmt.Sprintf("%d-%d-%d", now.Year(), now.Month(), now.Day())
}
func GetCurrentTimeYM() string {
	now := time.Now()
	return fmt.Sprintf("%d-%d", now.Year(), now.Month())
}
func GetCurrentTimeYMDDiagonal() string {
	now := time.Now()
	return fmt.Sprintf("%d/%d/%d", now.Year(), now.Month(), now.Day())
}
func GetCurrentYMDHMS() string {
	now := time.Now()
	return fmt.Sprintf("%d%d%d%d%d%d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
}

/*
*
返回之前的时间的格式化时间
*/
func GetBeforeTimeYMD(years int, months int, days int) string {
	now := time.Now()
	now = now.AddDate(-years, -months, -days)
	return fmt.Sprintf("%d-%d-%d", now.Year(), now.Month(), now.Day())
}
func GetBeforeTimeYMDDiagonal(years int, months int, days int) string {
	now := time.Now()
	now = now.AddDate(-years, -months, -days)
	return fmt.Sprintf("%d/%d/%d", now.Year(), now.Month(), now.Day())
}

func GetAfterTimeYMD(years int, months int, days int) string {
	now := time.Now()
	now = now.AddDate(years, months, days)
	return fmt.Sprintf("%d-%d-%d", now.Year(), now.Month(), now.Day())
}

func GetStringTime(date time.Time) string {
	return fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day())
}

func GetStringAllTime(date time.Time) string {
	return fmt.Sprintf("%d/%d/%d %d:%d:%d", date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second())
}

/*
没有预设CST时区,所以使用UTC+时间(秒)的方式表示UTC+8的方式
模板:2006-1-2 15:04:05  这个时间是固定的，不能改为其他时间
*/
func GetTimeStamp(date string) int64 {
	fixedZone := time.FixedZone("UTC", 28800)
	parse, err := time.ParseInLocation("2006-1-2 15:04:05", date, fixedZone)
	if err != nil {
		fmt.Println(err)
	}
	return parse.Unix()
}

// 字符串转换为时间
func StringToTimeYMD(ymd string) (time.Time, error) {
	fixedZone := time.FixedZone("UTC", 28800)
	parse, err := time.ParseInLocation("2006-1-2", ymd, fixedZone)
	if err != nil {
		fmt.Println(err)
	}
	return parse, nil
}

// getYearMonthToDay 查询指定年份指定月份有多少天
// @params year int 指定年份
// @params month int 指定月份
func GetYearMonthToDay(year int, month int) int {
	// 有31天的月份
	day31 := map[int]bool{
		1:  true,
		3:  true,
		5:  true,
		7:  true,
		8:  true,
		10: true,
		12: true,
	}
	if day31[month] == true {
		return 31
	}
	// 有30天的月份
	day30 := map[int]bool{
		4:  true,
		6:  true,
		9:  true,
		11: true,
	}
	if day30[month] == true {
		return 30
	}
	// 计算是平年还是闰年
	if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
		// 得出2月的天数
		return 29
	}
	// 得出2月的天数
	return 28
}

func GetCurrentTimeYMDHMS() string {
	date := time.Now()
	return fmt.Sprintf("%d%d%d%d%d%d", date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second())
}
